package com.david.warehouse.controller;

import com.david.warehouse.service.CategoruyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static com.david.common.utils.ResultUtils.fail;
import static com.david.common.utils.ResultUtils.success;

@RestController
@RequestMapping("/whm/category")
@Api(tags = "仓库管理-类目管理")
public class CategoryController {
    @Autowired
    private CategoruyService categoruyService;

    @GetMapping("/getType")
    @ApiOperation(value = "获取类目id")
    public ResponseEntity<Object> getTypeId(@RequestParam("type") String type){
        return success(true,categoruyService.getType(type));
    }

    @GetMapping("/{typeId}")
    @ApiOperation(value = "获取某类别下的所有类目")
    public ResponseEntity<Object> getCategoryList(@PathVariable Integer typeId){
        return success(true,categoruyService.getCategoryList(typeId));
    }

    @PutMapping("/add")
    @ApiOperation(value = "添加类目")
    public ResponseEntity<Object> addCategory(@RequestParam("name") String name,
                                              @RequestParam("typeId") Integer typeId,
                                              @RequestParam("parentId") Integer parentId){
        System.out.println("添加类目：" + name + "，父分类：" + parentId);
        categoruyService.addCategory(name,typeId, parentId);
        return success(true, "添加成功");
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "删除类目")
    public ResponseEntity<Object> deleteCategory(@PathVariable Integer id)
    {
        System.out.println("删除类目id：" + id);
        String result = categoruyService.deleteCategory(id);
        if (result.equals("completed")){
            return success(true, "删除成功");
        } else {
            return success(false, result);
        }

    }
    @PutMapping("/update")
    @ApiOperation(value = "修改类目名称")
    public ResponseEntity<Object> updateCategory(@RequestParam("id") Integer id,
                                                 @RequestParam("name") String name)
    {
        System.out.println("修改类目id：" + id + "，名称为：" + name);
        categoruyService.updateCategory(id,name);
        return success(true, "修改成功");
    }
}
